package com.sg.hms.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.jasper.tagplugins.jstl.core.ForEach;

import com.sg.hms.Model.Application;
import com.sg.hms.Model.Pagebean;
import com.sg.hms.Model.User;
import com.sg.hms.Service.ApplicationService;
import com.sg.hms.ServiceImpl.ApplicationServiceImpl;
/***
 * 查看一种申请
 * @author 吴磊
 *
 */
public class GetApplicationController extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("UTF-8");
		User user=(User)req.getSession().getAttribute("currentuser");
		String  state=req.getParameter("state");
		if (state!=null) {
			req.getSession().setAttribute("val", state);
		}
		ApplicationService app=new ApplicationServiceImpl();
		String nString=(String) req.getSession().getAttribute("val");
		ArrayList<Application> all=app.someApplication(user.getuId(), nString);
		String pString = req.getParameter("page");
		// 页码处理页码为空的时候默认为一
		int p = 0;
		if (pString == null) {
			p = 1;
		} else {
			p = Integer.parseInt(pString);
		}
		Pagebean pagebean=new Pagebean(p, 5);
		int size=all.size();
		ArrayList<Application> some=app.someApplication(user.getuId(), nString, pagebean);
		if (size<=5) {
			req.setAttribute("page1", null);
		}else {
			req.setAttribute("page1", pagebean.getthisPage4(size,p,pagebean.getPageSize()));
		}
		req.setAttribute("applications", some);
		req.getRequestDispatcher("page/demand.jsp").forward(req, resp);
		
	}

}
